package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.cderp.domain.CustomerSettleFeeSheet;
import com.ruoyi.cderp.domain.CustomerSettlePreSheet;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.CustomerSettleCheckSheetDetail;
import com.ruoyi.cderp.mapper.CustomerSettleCheckSheetMapper;
import com.ruoyi.cderp.domain.CustomerSettleCheckSheet;
import com.ruoyi.cderp.service.ICustomerSettleCheckSheetService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户对账单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class CustomerSettleCheckSheetServiceImpl implements ICustomerSettleCheckSheetService
{
    @Autowired
    private CustomerSettleCheckSheetMapper customerSettleCheckSheetMapper;

    /**
     * 查询客户对账单
     *
     * @param id 客户对账单主键
     * @return 客户对账单
     */
    @Override
    public CustomerSettleCheckSheet selectCustomerSettleCheckSheetById(String id)
    {
        //根据id查询客户对账单信息
        CustomerSettleCheckSheet customerSettleCheckSheet = customerSettleCheckSheetMapper.selectCustomerSettleCheckSheetById(id);
        //根据id查询客户对账单详情信息
        List<CustomerSettleCheckSheetDetail> customerSettleCheckSheetDetail = customerSettleCheckSheetMapper.selectFeeSheetCheckDetailIn(id);
        //定义一个set集合去重
        Set<Long> num = new HashSet<>();
        //循环遍历对账详情
        for (CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail1 : customerSettleCheckSheetDetail){
            //将对账详情的bizType添加到set集合中
            num.add(customerSettleCheckSheetDetail1.getBizType());
        }
        //定义一个空的对账单详情列表
        List<CustomerSettleCheckSheetDetail> customerSettleCheckSheetDetails = new ArrayList<>();
        //根据详情类型进行查询不同的业务类型添加到空的详情列表中
        for(Long num1 : num){
            if(num1 == 1){
                customerSettleCheckSheetDetails.addAll(customerSettleCheckSheetMapper.selectFeeSheetFeeSheetDetail(id));
            }
            if(num1 == 2){
                customerSettleCheckSheetDetails.addAll(customerSettleCheckSheetMapper.selectCustomerSettlePreDetail(id));
            }
//            if(num1 == 3){
//                customerSettleCheckSheetDetails.addAll(customerSettleCheckSheetMapper.selectTabSaleOutSheetDetail(id));
//            }
//            if(num1 == 4){
//                customerSettleCheckSheetDetails.addAll(customerSettleCheckSheetMapper.selectTabSaleReturnDetail(id));
//            }
        }
        //将自定义的详情列表添加到客户对账单中
        customerSettleCheckSheet.setCustomerSettleCheckSheetDetailList(customerSettleCheckSheetDetails);
        return customerSettleCheckSheet;
    }

    /**
     * 查询客户对账单列表
     *
     * @param customerSettleCheckSheet 客户对账单
     * @return 客户对账单
     */
    @Override
    public List<CustomerSettleCheckSheet> selectCustomerSettleCheckSheetList(CustomerSettleCheckSheet customerSettleCheckSheet)
    {
        return customerSettleCheckSheetMapper.selectCustomerSettleCheckSheetList(customerSettleCheckSheet);
    }

    /**
     * 新增客户对账单
     *
     * @param customerSettleCheckSheet 客户对账单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomerSettleCheckSheet(CustomerSettleCheckSheet customerSettleCheckSheet)
    {
        customerSettleCheckSheet.setCreateTime(DateUtils.getNowDate());

        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人编号
        customerSettleCheckSheet.setCreateById(loginUser.getUser().getUserId().toString());
        //创建人名称
        customerSettleCheckSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = customerSettleCheckSheetMapper.insertCustomerSettleCheckSheet(customerSettleCheckSheet);
        insertCustomerSettleCheckSheetDetail(customerSettleCheckSheet);
        return rows;
    }

    /**
     * 修改客户对账单
     *
     * @param customerSettleCheckSheet 客户对账单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomerSettleCheckSheet(CustomerSettleCheckSheet customerSettleCheckSheet)
    {
        customerSettleCheckSheet.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人编号
        customerSettleCheckSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        //修改人名称
        customerSettleCheckSheet.setUpdateBy(loginUser.getUser().getUserName());

        customerSettleCheckSheetMapper.deleteCustomerSettleCheckSheetDetailBySheetId(customerSettleCheckSheet.getId());
        insertCustomerSettleCheckSheetDetail(customerSettleCheckSheet);
        return customerSettleCheckSheetMapper.updateCustomerSettleCheckSheet(customerSettleCheckSheet);
    }

    /**
     * 批量删除客户对账单
     *
     * @param ids 需要删除的客户对账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleCheckSheetByIds(String[] ids)
    {
        customerSettleCheckSheetMapper.deleteCustomerSettleCheckSheetDetailBySheetIds(ids);
        return customerSettleCheckSheetMapper.deleteCustomerSettleCheckSheetByIds(ids);
    }

    /**
     * 删除客户对账单信息
     *
     * @param id 客户对账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleCheckSheetById(String id)
    {
        customerSettleCheckSheetMapper.deleteCustomerSettleCheckSheetDetailBySheetId(id);
        return customerSettleCheckSheetMapper.deleteCustomerSettleCheckSheetById(id);
    }

    /**
     * 查询客户的所有账单
     *
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param customerId 客户编号
     * @return 结果
     */
    @Override
    public List<CustomerSettleCheckSheetDetail> selectFeeSheetCheckDetailList(Date startDate, Date endDate, String customerId) {
        List<CustomerSettleCheckSheetDetail> list = new ArrayList<>();
        //客户收款
        List<CustomerSettleCheckSheetDetail> customerSettleFeeSheets = customerSettleCheckSheetMapper.selectFeeSheetFeeSheetList(startDate, endDate, customerId);
        list.addAll(customerSettleFeeSheets);
        //客户付款
        List<CustomerSettleCheckSheetDetail> customerSettlePreSheets = customerSettleCheckSheetMapper.selectCustomerSettlePreList(startDate, endDate, customerId);
        list.addAll(customerSettlePreSheets);
        //销售出库单
//        List<CustomerSettleCheckSheetDetail> tabSaleOutSheetList = customerSettleCheckSheetMapper.selectTabSaleOutSheetList(startDate, endDate, customerId);
//        list.addAll(tabSaleOutSheetList);
        //销售退单
//        List<CustomerSettleCheckSheetDetail> tabSaleReturnList = customerSettleCheckSheetMapper.selectTabSaleReturnList(startDate, endDate, customerId);
//        list.addAll(tabSaleReturnList);

        return list;
    }

    /**
     * 新增客户对账单明细信息
     *
     * @param customerSettleCheckSheet 客户对账单对象
     */
    public void insertCustomerSettleCheckSheetDetail(CustomerSettleCheckSheet customerSettleCheckSheet)
    {
        List<CustomerSettleCheckSheetDetail> customerSettleCheckSheetDetailList = customerSettleCheckSheet.getCustomerSettleCheckSheetDetailList();
        String id = customerSettleCheckSheet.getId();
        if (StringUtils.isNotNull(customerSettleCheckSheetDetailList))
        {
            List<CustomerSettleCheckSheetDetail> list = new ArrayList<CustomerSettleCheckSheetDetail>();
            for (CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail : customerSettleCheckSheetDetailList)
            {
                customerSettleCheckSheetDetail.setSheetId(id);

                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                customerSettleCheckSheetDetail.setId("170" + formattedDate+randomNumber);


                list.add(customerSettleCheckSheetDetail);
            }
            if (list.size() > 0)
            {
                customerSettleCheckSheetMapper.batchCustomerSettleCheckSheetDetail(list);
            }
        }
    }
}
